import {http, parse} from './util'

export const dics = {
  schoolNature: 'tab_schoolnature', // 学校性质
  affil: 'TAB_AFFIL', // 政治面貌
  sex: 'TAB_SEX',
  edu: 'TAB_EDUCATION', // 学历
  workType: 'TAB_WORKFORM', // 工作形式
  profession: 'TAB_PROFESSION', // 专业
  nation: 'TAB_NATION', // 民族
  people: 'TAB_PEOPLEBEHALF', // 政协委员
  reward: 'RSDA_TAB_REWARD', // 奖项名称
  degree: 'TAB_DEGREE', // 学位
  marriage: 'TAB_MARRIAGE', // 婚姻
  unitnature: 'TAB_UNITNATURE', // 机构类型
  economic: 'TAB_ECONOMIC', // 经济类型
  industry: 'TAB_INDUSTRY', // 所属行业
  craft: 'TAB_CRAFT', // 岗位类型
  decla: 'RSDA_TAB_DECLARATIONTABLE', // 缴费基数
  title: 'TAB_TITLE', // 职称
  titLevel: 'TAB_JOBTITLE', // 职称级别
  coq: 'TAB_COQ', // 国家职业资格等级（技能人员等级）
  languages: 'TAB_LANGUAGES', // 语种
  langLevel: 'RSDA_TAB_LANGUAGES_LEVEL', // 语种熟练程度
  eduType: 'TAB_CULTURETYPE', // 教育类别
  learn: 'RSDA_TAB_LEARN', // 学习形式
  family: 'RSDA_TAB_FAMILY_RELATION', // 家庭成员与本人关系
  punish: 'RSDA_TAB_PUNISHMENT', // 处分
  eduLevel: 'RSDA_TAB_CONTINUE_TECHNICAL', // 继续教育职称级别
  eduDegree: 'RSDA_TAB_CONTINUE_EDUCATION' // 继续教育学历
}

// 性别字典表
export const sexDic = [{value: '1', name: '男'}, {value: '2', name: '女'}]
// 政治面貌
export const affilDic = [{value: '01', name: '中共党员'}, {value: '02', name: '中共预备党员'}]

export function getDics(...args) {
  const arr = []
  args.forEach(name => {
    arr.push(new Promise((resolve, reject) => {
      const session = sessionStorage.getItem(name)
      if (session) {
        resolve(parse(session))
      } else {
        http({
          method: 'post',
          data: {
            tableName: name
          },
          url: '/sapi/get/dictionary/list'
        }).then(res => {
          const list = res.data.map(item => {
            return {
              name: item.NAME,
              value: item.CODE
            }
          })
          sessionStorage.setItem(name, JSON.stringify(list))
          resolve(list)
        }).catch(() => {
          resolve([])
        })
      }
    }))
  })
  return Promise.all(arr)
}

export function getCities() {
  const name = '__CITIES__'
  return new Promise((resolve, reject) => {
    const temp = sessionStorage.getItem(name)
    if (temp) {
      resolve(parse(temp))
    } else {
      http({
        method: 'post',
        data: {},
        url: '/sapi/get/city/getCityTree'
      }).then(res => {
        const list = res.data.children.map(item => {
          return {
            n: item.text,
            v: item.id,
            p: item.parentId,
            c: (item.children || []).map(item => {
              (item.children || []).forEach(val => {
                if (val.children && val.children.length) {
                  item.children = item.children.concat(val.children)
                }
              })
              return {
                n: item.text,
                v: item.id,
                p: item.parentId,
                c: (item.children || []).map(item => {
                  return {
                    n: item.text,
                    v: item.id,
                    p: item.parentId,
                    c: []
                  }
                })
              }
            })
          }
        })
        sessionStorage.setItem(name, JSON.stringify(list))
        resolve(list)
      }).catch(() => {
        resolve([])
      })
    }
  })
}

export function getTitleType() {
  const name = '__TITLE_TYPE__'
  return new Promise((resolve, reject) => {
    const list = sessionStorage.getItem(name)
    if (list) {
      resolve(parse(list))
    } else {
      http({
        method: 'post',
        data: {
          tableName: 'rsda_tab_title_type',
          valueField: 'pid,name',
          textField: 'code'
        },
        url: '/sapi/get/dictionary/list'
      }).then(res => {
        const results = res.data.map(item => {
          return {
            name: item.VALUEFIELD,
            value: item.TEXTFIELD,
            pid: item.PID
          }
        })
        sessionStorage.setItem(name, JSON.stringify(results))
        resolve(results)
      }).catch(() => {
        resolve([])
      })
    }
  })
}
